package dao.impl;

import dao.UserDao;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class UserDaoImpl implements UserDao {
    @Override
    public String select(String name, String password) throws Exception {
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();
        ResultSet rs = null;
        String idNumber = "null";
        try {
//            执行SQL语句
            String str = "SELECT * FROM user WHERE name = " + "'" + name + "'" + " and password = " + "'" + password + "'";
//            System.out.println(str);
            rs = stat.executeQuery(str);
            while(rs.next()) {
                idNumber = rs.getString("idNumber");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(rs,stat,conn);
        }
        return idNumber;
    }

    @Override
    public String selectMoney(String idNumber) throws Exception {
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();
        ResultSet rs = null;
        String money = null;
        try {
//            执行SQL语句
            String str = "SELECT * FROM user WHERE idNumber = " + "'" + idNumber + "'";
//            System.out.println(str);
            rs = stat.executeQuery(str);
            while(rs.next()) {
                money = rs.getString("money");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(rs,stat,conn);
        }
        return money;
    }

    @Override
    public String insert(String name, String password, String identity) throws Exception {
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();
        try {
//            执行SQL语句
            String idNumber = String.valueOf((int)(Math.random()*100000000));
            String str = "INSERT INTO user(idNumber,name,identity,password) VALUES ('" + idNumber +"','"+ name+"','"+ identity +"','"+ password +"')";
//            System.out.println(str);
            stat.executeUpdate(str);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }
        return identity;
    }

    @Override
    public String updateMoney(String idNumber, String money) throws Exception {
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();
        try {
//            执行SQL语句
            String str = "UPDATE user SET money = " + money + " WHERE idNumber = '"+ idNumber + "'";
//            System.out.println(str);
            stat.executeUpdate(str);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }
        return null;
    }
}
